const appInstance = getApp()
const request = require('../../utils/request.js');
Page({
  data: {
    username: '',
    password: '',
    confirmPassword: '',
    phone: '',
    email: '',
    isAgree: false,
    canGetCode: true,
    countdown: 0
  },

  onUsernameInput(e) {
    this.setData({
      username: e.detail.value
    })
  },

  onPasswordInput(e) {
    this.setData({
      password: e.detail.value
    })
  },

  onConfirmPasswordInput(e) {
    this.setData({
      confirmPassword: e.detail.value
    })
  },

  onPhoneInput(e) {
    this.setData({
      phone: e.detail.value
    })
  },

  onEmailInput(e) {
    this.setData({
      email: e.detail.value
    })
  },

  onAgreementChange(e) {
    this.setData({
      isAgree: e.detail.value.length > 0
    })
  },

  handleRegister() {
    const { username, code, password, confirmPassword, phone, email, isAgree } = this.data

    if (!isAgree) {
      wx.showToast({
        title: '请先同意用户协议和隐私政策',
        icon: 'none'
      })
      return
    }

    if (!username) {
      wx.showToast({
        title: '请输入用户名',
        icon: 'none'
      })
      return
    }

    if (username.length < 4 || username.length > 20) {
      wx.showToast({
        title: '用户名长度必须在4-20个字符之间',
        icon: 'none'
      })
      return
    }

    if (!password) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
      return
    }

    if (password.length < 6) {
      wx.showToast({
        title: '密码长度不能少于6位',
        icon: 'none'
      })
      return
    }

    if (password !== confirmPassword) {
      wx.showToast({
        title: '两次输入的密码不一致',
        icon: 'none'
      })
      return
    }

    // 表单验证
    if (!phone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return
    }

    if (!/^1[3-9]\d{9}$/.test(phone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      })
      return
    }

    // 显示加载中
    wx.showLoading({
      title: '注册中...',
    })

    // 调用注册接口
    request.request({
      url: appInstance.globalData.baseUrl + '/api/auth/register',
      method: 'POST',
      data: {
        username,
        password,
        confirmPassword,
        phone,
        email
      },
      success: (res) => {
        if (res.data.code === 200) {
          wx.showToast({
            title: '注册成功',
            icon: 'success'
          })

          // 注册成功后跳转到登录页
          setTimeout(() => {
            wx.redirectTo({
              url: '/pages/login/login'
            })
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.message || '注册失败',
            icon: 'none'
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误，请重试',
          icon: 'none'
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },

  goToLogin() {
    wx.navigateBack()
  },

  showAgreement() {
    wx.navigateTo({
      url: '/pages/agreement/agreement'
    })
  },

  showPrivacy() {
    wx.navigateTo({
      url: '/pages/privacy/privacy'
    })
  }
}) 